package com.astar.controller;


import com.astar.pojo.Admin;
import com.astar.pojo.AdminLogin;
import com.astar.pojo.vo.RespBean;
import com.astar.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author weishen
 * @since 2021-06-21
 */
@Api(tags = "AdminController")
@RestController
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @ApiOperation("登录成功之后返回token")
    @PostMapping("/login")
    public RespBean getToken(@RequestBody AdminLogin adminLogin, HttpServletRequest request){
        return adminService.login(adminLogin.getUsername(),adminLogin.getPassword(),adminLogin.getCode(),request);
    }

    @ApiOperation("根据用户名查询用户信息")
    @PostMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        //从security全局内容中获取用户名
        if(null==principal){
            return null;
        }
        Admin admin = adminService.findAdminByUserName(principal.getName());
        admin.setPassword(null);
        return admin;
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("退出成功！");
    }
}
